#!/usr/bin/python
import pexpect

class PythonInterpreter(object):
    def __init__(self, forbidden=[], quiet=True):
        self.forbidden = forbidden
        self.eof = False
        self.lastline = None
        self.py = pexpect.spawn('python')
        self.eoc = "# __end_of_command__"
        if quiet:
            [self.readline() for i in range(3)]  # Chew version stuff
        self.cmd_history = [
                'print "Happy python interpreter reporting for duty."']

    def readline(self):
        self.lastline = self.py.readline().rstrip()
        return self.lastline

    def prompt(self):
        if self.eco in self.lastline:
            return self.lastline[:self.lastline.index(self.eoc)]
        return ">>>"

    def command(self, index):
        return self.cmd_history[index]

    def process(self, cmd):
        lines = []
        self.cmd_history.append(cmd)
        if self.eof:
            lines.append('** PythonInterpreter:  EOF')
        try:
            if any([item in cmd for item in self.forbidden]):
                return ['    "%s"' % cmd, '  Disallowed.  Sorry.']
            self.py.sendline(cmd + "\n" + self.eoc)
            garbage, line = self.readline(), self.readline()
            while self.eoc not in line:
                lines.append(line)
                line = self.readline()
        except Exception, e:
            lines.append( '** PythonInterpreter:  There was an error.' )
            self.eof = True
        return lines

# Quick little test:
if __name__ == '__main__':
    interp = PythonInterpreter(forbidden=['import'])
    while not interp.eof:
        cmd = raw_input(interp.prompt())
        lines = interp.process(cmd)
        for line in lines:
            print line
    print " Goodbye."
